package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.service.UserService;
import com.itheima.util.JwtUtils;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController

public class LoginController {

    @Autowired
    private EmpService empService;

    @Autowired
    private UserService userService;

    //员工登录
    @PostMapping("/login")
    public Result login(@RequestBody Map<String,String> map){
        //1. 接收参数
        String username = map.get("username");
        String password = map.get("password");

        //2. 盗用service查询账号\密码
        Emp emp = userService.findBuyUsernameAndPassword(username, password);

        //3. 判断
        if (emp == null){
            return Result.error("用户名不存在或密码错误");
        }else {
            //1. 生成token
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("id",emp.getId());
            claims.put("username",emp.getUsername());
            String jwt = JwtUtils.generateJwt(claims);

            //2. 返回token
            return Result.success(jwt);
        }
    }




}
